<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :obj="companyInfo" :is-root="true" @add-depts="onAdd(companyInfo)" />
        <el-tree :data="treeData" :default-expand-all="true" :props="defaultProps">
          <!-- {{ node.label }} -->
          <tree-tools slot-scope="{ data }" :obj="data" @add-edit="onEdit(data)" @add-depts="onAdd(data)" />
        </el-tree>
      </el-card>
      <add-dept ref="depts" v-model="addDialogVisible" :node="treeNode" @edit-success="loadDepartments" @add-success="loadDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartmentsList } from '@/api/departments.js'
import { transListToTree } from '@/utils'
import AddDept from './components/add-dept.vue'

export default {
  components: { TreeTools, AddDept },
  data() {
    return {
      addDialogVisible: false,
      treeData: [],
      defaultProps: {
        label: 'name'
      },
      companyInfo: {
        name: '传智播客',
        manager: '负责人',
        id: ''
      },
      treeNode: {}
    }
  },

  created() {
    this.loadDepartments()
  },

  methods: {
    // 查询企业部门列表
    async loadDepartments() {
      const res = await getDepartmentsList()
      this.treeData = transListToTree(res.depts, '')
    },
    onAdd(data) {
      this.addDialogVisible = true
      this.treeNode = data
    },
    onEdit(data) {
      this.addDialogVisible = true
      this.treeNode = data
      this.$refs.depts.getDeptsInfo(data.id)
    }
  }
}
</script>

<style scoped lang='scss'>
.el-row {
  width: 100%;
}
</style>
